﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

namespace AAEngine.Engine.Entities
{
    public abstract class EntityComponent : IEntityComponent
    {
        public IEntity Owner
        {
            get;
            set;
        }

        public string Name
        {
            get;
            protected set;
        }

        public bool IsRegistered
        {
            get;
            protected set;
        }


        public void Register(IEntity owner, string name)
        {
            if (IsRegistered)
            {
                throw new AAException("Trying to register an already-registered component!");
            }

            Owner = owner;
            Name = name;
            OnAdd();
            IsRegistered = true;
        }

        public void Unregister()
        {
            if (!IsRegistered)
            {
                throw new AAException("Trying to unregister an unregistered component!");
            }

            IsRegistered = false;
            OnRemove();
            Owner = null;
            Name = null;
        }

        public void Reset()
        {
            OnReset();
        }

        /// <summary>
        /// This is called when the component is added to an entity. Any initialization,       
        /// event registration, or object lookups should happen here. Component lookups       
        /// on the owner entity should NOT happen here. Use OnReset instead.
        /// </summary>
        public virtual void OnAdd() { }

        /// <summary>
        /// This is called when the component is removed from an entity. It should reverse       
        /// anything that happened in onAdd or onReset (like removing event listeners or       
        /// nulling object references).
        /// </summary>
        public virtual void OnRemove() { }

        /// <summary>
        /// This is called anytime a component is added or removed from the owner entity.       
        /// Lookups of other components on the owner entity should happen here.
        /// </summary>
        /// <remarks>
        /// This can potentially be called multiple times, so make sure previous lookups       
        /// are properly cleaned up each time.
        /// </remarks>
        public virtual void OnReset() { }
    }
}
